package com.his.dictitem.service.Imp;

import com.his.dictitem.dao.DictItemCategoryMapper;
import com.his.dictitem.entity.DictItemCategory;
import com.his.dictitem.entity.DictItemCategoryKey;
import com.his.dictitem.service.DictItemCategoryService;
import com.his.dictitem.vo.DictItemCategoryVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictItemCategoryServiceImpl implements DictItemCategoryService {

    @Autowired
    private DictItemCategoryMapper dictItemCategoryMapper;

    @Override
    public void insert(DictEmployee dictEmployee, DictItemCategoryVo itemCategoryVo) {
        DictItemCategory dictItemCategory= BeanUtil.copyBean(itemCategoryVo,DictItemCategory.class);
        dictItemCategory.setHospid(dictEmployee.getHOSPID());
        dictItemCategory.setId(getCategoryNewID(dictItemCategory));
        dictItemCategoryMapper.insert(dictItemCategory);
    }

    @Override
    public void deleteByKey(DictEmployee dictEmployee,String cateid) {
        DictItemCategoryKey ckey=new DictItemCategoryKey();
        ckey.setId(cateid);
        ckey.setHospid(dictEmployee.getHOSPID());
        dictItemCategoryMapper.deleteByPrimaryKey(ckey);
    }

    @Override
    public void updateByKeySelective(DictEmployee dictEmployee,DictItemCategoryVo itemCategoryVo) {
        DictItemCategory dictItemCategory= BeanUtil.copyBean(itemCategoryVo,DictItemCategory.class);
        dictItemCategory.setHospid(dictEmployee.getHOSPID());
        dictItemCategoryMapper.updateByPrimaryKeySelective(dictItemCategory);
    }

    @Override
    public List<DictItemCategory> queryBySelective(DictEmployee dictEmployee,DictItemCategoryVo itemCategoryVo) {
        DictItemCategory dictItemCategory= BeanUtil.copyBean(itemCategoryVo,DictItemCategory.class);
        dictItemCategory.setHospid(dictEmployee.getHOSPID());
        return dictItemCategoryMapper.selectBySelective(dictItemCategory);
    }


    /**
     * 根据父ID     生成新的ID
     * @param itemCategory 新增目录信息
     * @return 新目录ID
     */
    private String getCategoryNewID(DictItemCategory itemCategory){
        DictItemCategory dictItemCategory=new DictItemCategory();
        dictItemCategory.setHospid(itemCategory.getHospid());
        dictItemCategory.setGrade(itemCategory.getGrade());
        if("".equals(itemCategory.getPid())||"0".equals(itemCategory.getPid())||null==itemCategory.getPid()){
            //没有父级目录 取顶层目录(取消父级ID过滤的限制 层级为1的所有根目录)
            //设置父节点为""  后续逻辑需要
            itemCategory.setPid("");
            if(itemCategory.getGrade()!=1){
                return "err";
            }
        }else{
            //有父级目录  取其所有子目录
            dictItemCategory.setPid(itemCategory.getPid());
        }
        //获取排序最大的子目录  生成新的ID
        List<DictItemCategory> ditList=dictItemCategoryMapper.selectBySelective(dictItemCategory);
        int maxIdex=0;
        for(DictItemCategory dictItemCategorySel:ditList){
            String cid=dictItemCategorySel.getId();
            int cindex=Integer.parseInt(cid.substring(itemCategory.getPid().length()));
            System.out.println(cindex);
            if(cindex>maxIdex){
                maxIdex=cindex;
            }
        }
        int newIndex=maxIdex+1;
        String newId=itemCategory.getPid();
        if(newIndex>0&&newIndex<10){
            newId=newId+"0"+newIndex;
        }else if(newIndex>9){
            newId=newId+newIndex;
        }
        return newId;
    }
}
